﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangLego.Bussiness;
using CuaHangLego.Model;
using System.Threading;

namespace CuaHangLego.UI
{
    public partial class QLHoaDonUI : UserControl
    {
        private Lazy<HoaDonBanBSS> hdbbss;
        private Lazy<HoaDonMuaBSS> hdmbss;
        private Lazy<SanPhamKhoBSS> spbss;
        private int idx1 = -1;
        private int idx2 = -1;

        public QLHoaDonUI()
        {
            InitializeComponent();
            tblHDB.AutoGenerateColumns = false;
            tblHDM.AutoGenerateColumns = false;
            tblCTHDM.AutoGenerateColumns = false;
            tblCTHDB.AutoGenerateColumns = false;
            this.spbss = new Lazy<SanPhamKhoBSS>(() => new SanPhamKhoBSS());
            this.hdmbss = new Lazy<HoaDonMuaBSS>(() => new HoaDonMuaBSS());
            this.hdbbss = new Lazy<HoaDonBanBSS>(() => new HoaDonBanBSS());
        }

        private void QLHoaDonUI_Load(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void buttonThemHoaDonMua_Click(object sender, EventArgs e)
        {
            spbss.Value.HideInVisible();
            HoaDonMuaDetail frm = new HoaDonMuaDetail(spbss.Value, hdmbss.Value, null);
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ShowDialog();
            hdmbss.Value.UpdateData();
            tblHDM.DataSource = hdmbss.Value.ListHD;
        }

        private void tblHDM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ShowChangeHDM();
        }

        private void buttonSuaHDM_Click(object sender, EventArgs e)
        {
            if (tblHDM.SelectedRows.Count > 0)
            {
                ShowChangeHDM();
            }
        }


        private void tblHDM_SelectionChanged(object sender, EventArgs e)
        {
            if (tblHDM.SelectedRows.Count > 0)
            {
                if (tblHDM.SelectedRows[0].Index != idx2)
                {
                    idx2 = tblHDM.SelectedRows[0].Index;
                    HoaDonMua selectedhd = tblHDM.SelectedRows[0].DataBoundItem as HoaDonMua;
                    labelMaHDM.Text = selectedhd.MaSo.ToString();
                    labelNCC.Text = selectedhd.NhaCungCap.Ten;
                    labelThoiGianM.Text = selectedhd.ThoiGian.ToString();
                    tblCTHDM.DataSource = selectedhd.DsCTHD;
                    txtTongMua.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000",
                        selectedhd.DsCTHD.Sum(ct => ct.SoLuong * ct.SanPham.GiaMua));
                }
            }
        }

        private void tblHDB_SelectionChanged(object sender, EventArgs e)
        {
            if (tblHDB.SelectedRows.Count > 0)
            {
                if (tblHDB.SelectedRows[0].Index != idx1)
                {
                    idx1 = tblHDB.SelectedRows[0].Index;
                    HoaDonBan selectedhd = tblHDB.SelectedRows[0].DataBoundItem as HoaDonBan;
                    labelMaHDB.Text = selectedhd.MaSo.ToString();
                    labelKhachHang.Text = selectedhd.KhachHang.HoTen;
                    labelThoiGianB.Text = selectedhd.ThoiGian.ToString();
                    tblCTHDB.DataSource = selectedhd.DsCTHD;
                    txtTongBan.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000",
                        selectedhd.DsCTHD.Sum(ct => ct.SoLuong * ct.SanPham.GiaBan));
                }
            }
        }

        private void tblHDB_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ShowChangeHDB();
        }



        private void buttonXacNhanTT_Click(object sender, EventArgs e)
        {
            if (tblHDB.SelectedRows.Count > 0)
            {
                ShowChangeHDB();
            }
        }

        private void ShowChangeHDB()
        {
            HoaDonBan hd = tblHDB.SelectedRows[0].DataBoundItem as HoaDonBan;
            DialogResult r = DialogResult.None;
            if (!hd.DaThanhToan)
            {
                r = MessageBox.Show("Đồng ý thanh toán cho hóa đơn này?", "Xác nhận thanh toán", MessageBoxButtons.OKCancel);
            }
            else r = MessageBox.Show("Đồng ý hủy thanh toán cho hóa đơn này?", "Xác nhận thanh toán", MessageBoxButtons.OKCancel);
            switch (r)
            {
                case DialogResult.OK:
                    IList<SanPham> list = hdbbss.Value.CheckQuantity(hd);
                    if (list.Count > 0 && !hd.DaThanhToan)
                    {
                        string err = "Sản phẩm không đủ để xử lý:";
                        foreach (SanPham sp in list)
                        {
                            err += "\n+" + sp.Ten;
                        }
                        MessageBox.Show(err, "Thông báo xử lý hóa đơn");
                    }
                    else
                    {
                        hd.DaThanhToan = !hd.DaThanhToan;
                        hdbbss.Value.Edit(hd);
                        hdbbss.Value.Commit();
                        hdbbss.Value.UpdateData();
                        tblHDB.DataSource = null;
                        tblHDB.DataSource = hdbbss.Value.ListHD;
                    }
                    break;
                case DialogResult.Cancel:
                    break;
                default:
                    break;
            }
        }

        private void ShowChangeHDM()
        {
            HoaDonMuaDetail frm = new HoaDonMuaDetail(spbss.Value, hdmbss.Value, tblHDM.SelectedRows[0].DataBoundItem as HoaDonMua);
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ShowDialog();
            hdmbss.Value.UpdateData();
            tblHDM.DataSource = hdmbss.Value.ListHD;
        }

        private void buttonCapNhatHDB_Click(object sender, EventArgs e)
        {
            hdbbss.Value.UpdateData();
            tblHDB.DataSource = hdbbss.Value.ListHD;
        }

        private void buttonCapNhatHDM_Click(object sender, EventArgs e)
        {
            hdmbss.Value.UpdateData();
            tblHDM.DataSource = hdmbss.Value.ListHD;
        }

        private void buttonSearch1_Click(object sender, EventArgs e)
        {
            tblHDB.DataSource=hdbbss.Value.GetHoaDonBanBy(txtKeyWord1.Text, calHDBTu.Value, calHDBDen.Value);
        }

        private void buttonAll1_Click(object sender, EventArgs e)
        {
            tblHDB.DataSource = hdbbss.Value.GetHoaDonBanAll();
        }

        private void buttonSearch2_Click(object sender, EventArgs e)
        {
            tblHDM.DataSource = hdmbss.Value.GetHoaDonMuaBy(txtKeyWord2.Text, calHDMTu.Value, calHDMDen.Value);
        }

        private void buttonAll2_Click(object sender, EventArgs e)
        {
            tblHDM.DataSource = hdmbss.Value.GetHoaDonMuaAll();
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            if (tblHDB.SelectedRows.Count > 0)
            {
                hdbbss.Value.Export(tblHDB.SelectedRows[0].DataBoundItem as HoaDonBan);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (tblHDM.SelectedRows.Count > 0)
            {
                hdmbss.Value.Export(tblHDM.SelectedRows[0].DataBoundItem as HoaDonMua);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            object a = hdbbss.Value.ListHD;
            object b = hdmbss.Value.ListHD;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.tblHDB.DataSource = hdbbss.Value.ListHD;
            this.tblHDM.DataSource = hdmbss.Value.ListHD;
        }
    }
}
